package cn.stylefeng.guns.sys.modular.cleanRule.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.cleanRule.entity.CleanRule;
import cn.stylefeng.guns.sys.modular.cleanRule.mapper.CleanRuleMapper;
import cn.stylefeng.guns.sys.modular.cleanRule.model.params.CleanRuleParam;
import cn.stylefeng.guns.sys.modular.cleanRule.model.result.CleanRuleResult;
import cn.stylefeng.guns.sys.modular.cleanRule.service.CleanRuleService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 数据清洗规则表 服务实现类
 * </p>
 *
 * @author author
 * @since 2020-11-16
 */
@Service
public class CleanRuleServiceImpl extends ServiceImpl<CleanRuleMapper, CleanRule> implements CleanRuleService {

    @Override
    public void add(CleanRuleParam param) {
        CleanRule entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CleanRuleParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(CleanRuleParam param) {
        CleanRule oldEntity = getOldEntity(param);
        CleanRule newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CleanRuleResult findBySpec(CleanRuleParam param) {
        return null;
    }

    @Override
    public List<CleanRuleResult> findListBySpec(CleanRuleParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(CleanRuleParam param) {
        param.setDataIsDelete(1);
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void deleteCleanRuleInfo(Integer id) {
        this.baseMapper.deleteCleanRule(id);
    }

    private Serializable getKey(CleanRuleParam param) {
        return param.getDataCleanRuleId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CleanRule getOldEntity(CleanRuleParam param) {
        return this.getById(getKey(param));
    }

    private CleanRule getEntity(CleanRuleParam param) {
        CleanRule entity = new CleanRule();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
